import GameConfig from "./GameConfig";

cc.Class({
    extends: cc.Component,

    properties: {
        type: 0,
    },

    onLoad () {

    },

    init(game) {
        this.game = game;
        this.node.opacity = 255;
        this.node.active = true;
        this.posY = GameConfig.INIT_BARRIER_POSY + GameConfig.BARRIER_UNIT_DISTANCE * 0.8;
    },

    // start方法只有在new的时候会调用
    start() {
        
    },

    // 碰撞检测
    onCollisionEnter(other,self){   
        var player = this.game.player;
        this._back2Pool();
        if (player.status == 0) {
            if (CC_WECHATGAME) {
                wx.vibrateLong({}); // 长震动
            }
            this.game.gameOver();
        } else {
            // 减少防护
            this.game.player.reduceLifeCount();
        }
    },

    _back2Pool() {
        this.game.dartBox[this.type].pool.put(this.node);
    },

    _move(posX, posY){
        this.node.setPosition(cc.v2(posX, posY));
    },

    update(dt) {
        if (GameConfig.GAME_START) {
            this.posY -= GameConfig.PROPS_PLAYER_MOVE_SPEED * 1.8;
            this._move(this.node.position.x, this.posY);
            // 超出显示范围，则放回池中
            if (this.posY <= -GameConfig.INIT_BARRIER_POSY) {
                this._back2Pool();
            }
        } else {
            if (GameConfig.GAME_OVER_LAYER_DISPLAY) {
                this.node.active = false;
            }
        }
    },
});
